package cn.edu.ctbu.myfirstweb.webapi;

import cn.edu.ctbu.myfirstweb.domain.*;
import cn.edu.ctbu.myfirstweb.helper.R;
import cn.edu.ctbu.myfirstweb.helper.RUtil;
import cn.edu.ctbu.myfirstweb.service.SCService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * 本api用于为学生端的的选课管理提供插入和删除方法
 */

@Slf4j
@RestController
public class StuSelectCourseRestController {

    @Qualifier("SCImpService")
    @Autowired
    SCService scService;

    /**.**
     * @Description: 通过request获取cid，在选课表SC中插入数据
     * @Param:  request
     * @Return: R
     * @Author: HighJ
     * @Date Created in
     */


    @GetMapping("/addSC")
    public R<?> addSC(HttpServletRequest request) {
        Student student = (Student) request.getSession().getAttribute("SYSTEM_USER_SESSION");//通过Session获取学生信息
        long cid = Integer.parseInt(request.getParameter("CID"));//获取前端传来的CID参数
        SCID  scid = new SCID(student.getNum(),cid);
        SC sc = new SC();
        sc.setScid(scid);
        sc.setFlag(0);//falg=0表示为本期所选课程，该课程正在进行中
        if (scService.getBySCID(scid) == null) {
            scService.insert(sc);
            return RUtil.success(sc);
        }
        return RUtil.error(520,"Error,this SC you want to insert is exists.");
    }

    /**.**
    * @Description: 根据前端的传来的cid删除SC表中的对应的表项
    * @Param: request
    * @Return: R
    * @Author: HighJ
    * @Date Created in
    */

    @DeleteMapping ("/deleteSC")
    public R<?> deleteSC(HttpServletRequest request) {
        Student student = (Student) request.getSession().getAttribute("SYSTEM_USER_SESSION");
        long cid = Integer.parseInt(request.getParameter("deleteCID"));
        SCID  scid = new SCID(student.getNum(),cid);
        if(scService.getBySCID(scid) != null) {
            scService.delete(scid);
            return RUtil.success();
        }
        return RUtil.error(520,"Error,this SC you want to delete is null.");
    }

}
